package net.pugsplace.gpremacy.client.text;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;

import net.pugsplace.gpremacy.packets.Chatter;
import net.pugsplace.gpremacy.packets.Packet;
import net.pugsplace.gpremacy.packets.PlayerInformation;
import net.pugsplace.gpremacy.rules.RuleSet;
import net.pugsplace.gpremacy.rules.UnitType;
import net.pugsplace.gpremacy.state.ResourceTuple;

public class TextClient {
	public static void main(String args[]) throws Exception {
		if (args.length < 2) {
			System.err.println("Arguments: [hostname] [port]");
			return;
		}

		String host = args[0];
		int port = Integer.parseInt(args[1]);

		TextClient tc = new TextClient(host, port);
		tc.connect();
		tc.run();
	}

	String host;
	ObjectInputStream ois;
	ObjectOutputStream oos;
	int port;
	Socket sock;

	public TextClient(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public void connect() throws Exception {
		System.out.print("Connecting to " + host + ":" + port);
		sock = new Socket(host, port);
		System.out.println("... connected.");
		oos = new ObjectOutputStream(sock.getOutputStream());
		ois = new ObjectInputStream(sock.getInputStream());

		System.out.print("Starting thread");
		new Thread(new Processor()).start();
		System.out.println("... done.");
	}

	public void run() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Enter player name:");
		PlayerInformation pi = new PlayerInformation();
		pi.playerName = br.readLine();
		oos.writeObject(pi);

		while (true) {
			System.out.println("Write line?");
			String s = br.readLine();
			oos.writeObject(new Chatter(s));
		}
	}

	public class Processor implements Runnable {
		public void run() {
			while (true) {
				try {
					Object o = ois.readObject();
					if (o instanceof Packet) {
						System.out.println("Received " + o);

						if (o instanceof Chatter) {
							Chatter c = (Chatter) o;
							SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
							System.out.println("[" + sdf.format(c.time.getTime()) + "] " + c.sender + ": " + c.text);
						}
						if (o instanceof net.pugsplace.gpremacy.rules.RuleSet) {
							RuleSet rs = (RuleSet) o;
							for (UnitType u : rs.getUnits()) {
								System.out.println(u.getType());
								System.out.print("\tUpkeep: ");
								for (ResourceTuple rt : u.getUpkeepCost())
									System.out.print(rt + " ");
								System.out.print("\n\tBuild cost: ");
								for (ResourceTuple rt : u.getBuildCost())
									System.out.print(rt + " ");
								System.out.println();
							}
						}
					} else {
						System.err.println("Unknown data received: " + o);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
